const { Model, DataTypes } = require("sequelize");

class OrdersCunqu extends Model {
  static init(sequelize) {
    super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        orderNum: {
          type: DataTypes.STRING(30),
          allowNull: false,
          comment: "订单号",
        },
        cabinetNum: {
          type: DataTypes.INTEGER,
          allowNull: false,
          comment: "快递柜编号",
        },
        cunId: {
          type: DataTypes.INTEGER,
          allowNull: false,
          comment: "存件人ID",
        },
        quId: {
          type: DataTypes.INTEGER,
          allowNull: false,
          comment: "取件人ID",
        },
        state: {
          type: DataTypes.STRING(10),
          allowNull: false,
          defaultValue: "待取", // 默认状态为 '待取'
          comment: "订单状态（待取,已取）",
        },
        createdAt: {
          type: DataTypes.DATE,
          allowNull: false,
          comment: "创建时间",
        },
        updatedAt: {
          type: DataTypes.DATE,
          allowNull: false,
          comment: "更新时间",
        },
        cabinetBelong: {
          // 新增字段
          type: DataTypes.STRING(60),
          allowNull: false,
          comment: "所属柜组",
        },
        quCode: {
          type: DataTypes.STRING(10),
          comment: '六位取件码'
        }
      },
      {
        sequelize,
        modelName: "OrdersCunqu",
        tableName: "orders_cunqu",
        timestamps: false, // 禁止自动管理 createdAt 和 updatedAt
        comment: "存取订单表",
      }
    );
  }

  static associate(models) {
    // 定义关联关系
    this.belongsTo(models.Users, {
      foreignKey: "cunId", // 外键字段
      targetKey: "id", // 关联 Users 表的 id 字段
      as: "cunUser", // 关联别名
    });

    this.belongsTo(models.Users, {
      foreignKey: "quId", // 外键字段
      targetKey: "id", // 关联 Users 表的 id 字段
      as: "quUser", // 关联别名
    });

    this.belongsTo(models.Cabinet, {
      // 新增关联
      foreignKey: "cabinetBelong", // 新增字段作为外键
      targetKey: "belong", // 关联 Cabinet 表的 belong 字段
      as: "cabinetBelongRef", // 关联别名
    });
  }
}

module.exports = OrdersCunqu;
